package com.niit.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

import java.lang.reflect.Method;

// 横切关注点--->切面(Aspect)  Aop中的A
public class LogAspect {

    public void beforeLog(JoinPoint joinPoint){
        System.out.println(joinPoint.getSignature().getName()+"方法执行开始....");
    }

    public void afterLog(JoinPoint joinPoint){
        System.out.println(joinPoint.getSignature().getName()+"方法执行结束....");
    }

    public void afterReturningLog(JoinPoint joinPoint){

        System.out.println("方法签名:"+ joinPoint.getSignature());
        System.out.println("方法参数："+ joinPoint.getArgs()[0]);

        //Method method = (Method) joinPoint.getTarget();
        System.out.println("方法返回结束....");
    }

    public void afterThrowingLog(JoinPoint joinPoint){
        System.out.println(joinPoint.getSignature().getName()+"方法发生异常....");
//        Method m = (Method) joinPoint.getTarget();

    }

    public void aroundLog(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("目标方法:get开头的方法执行之前，需要做的事情，写这里");
        System.out.println("around:"+joinPoint.getSignature().getName()+"执行开始");
        System.out.println("around:参数："+joinPoint.getArgs()[0]);

        Object result = joinPoint.proceed(); // 执行目标方法  调用那些get开头的方法

        System.out.println("around:返回值: "+result);
        System.out.println("目标方法:get开头的方法执行之后，需要做的事情，写这里");
    }
}
